﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;

using System.Data;
using System.Data.Common;


namespace TractRecord.Master
{
    public partial class TractRecord : System.Web.UI.MasterPage
    {
        private LogWriter logger;

        protected void Page_Load(object sender, EventArgs e)
        {
            logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

            Process_LoginLogout();
            DisplayHideButtons();

            if (Membership.GetUser() != null)
            {
                if (Roles.IsUserInRole("Administrator"))
                {
                    SiteMapDataSource1.StartingNodeUrl = "/Administration/Default.aspx"; 
                }
                else
                {
                    SiteMapDataSource1.StartingNodeUrl = "/Authenticated/Default.aspx";
                    //SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
                }
            }
        }

        SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            //SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            //SiteMapNode tempNode = currentNode;

            //if (Roles.IsUserInRole("Project Administrator"))
            //{

            //    SiteMapNode projects = new SiteMapNode(SiteMap.Provider, "/Authenticated/Projects.aspx", "/Authenticated/Projects.aspx", "Projects", "All project settings.");


            //    DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectSummaryForUser_Get");
            //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", Membership.GetUser().ProviderUserKey.ToString()));
            //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ActiveInd", true));

            //    IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

            //    while (r.Read())
            //    {
            //        projects.ChildNodes.Add(new SiteMapNode(SiteMap.Provider,"/Authenticated/Project.aspx?id=" + r["ProjectId"].ToString(), "/Authenticated/Project.aspx?id=" + r["ProjectId"].ToString(), r["Name"].ToString(), r["Description"].ToString()));
            //    }

            //    projects.ParentNode = tempNode;

            //    return projects;
            //}

            SiteMapNode projects = new SiteMapNode(SiteMap.Provider, "/Authenticated/Projects.aspx", "/Authenticated/Projects.aspx", "Projects", "All project settings.");

            return projects;
        }
        
        private void Process_LoginLogout()
        {
            if (Request.Form["name"] != null && Request.Form["password"] != null)
            {
                if (Membership.ValidateUser(Request.Form["name"], Request.Form["password"]))
                {
                    Code.Common.Log("User: " + Request.Form["name"] + " logged in successfully.", "Audit");
                    //FormsAuthentication.Authenticate(Request.Form["name"], Request.Form["password"]);
                    FormsAuthentication.SetAuthCookie(Request.Form["name"], true);

                    Response.Redirect("/Authenticated/Default.aspx");
                }
                else
                {
                    Code.Common.Log("Failed login for User: " + Request.Form["name"], "Audit");
                }
            }
            else if (Request.Form["btnLogout"] != null)
            {
                string name = Membership.GetUser().UserName;

                FormsAuthentication.SignOut();
                Code.Common.Log("User: " + name + " logged out successfully.", "Audit");
                Response.Redirect("~/Default.aspx");
            }
        }

        private void DisplayHideButtons()
        {
            if (Membership.GetUser() == null)
            {
                btnLogin.Visible = true;
                btnLogout.Visible = false;
                btnNewUser.Visible = true;
                btnAccount.Visible = false;
            }
            else
            {
                btnLogin.Visible = false;
                btnLogout.Visible = true;
                btnNewUser.Visible = false;
                btnAccount.Visible = true;
            }
        }

        

    }
}